package com.czz.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czz.portal.entity.CourseConsult;
import com.czz.portal.entity.Like;
import com.czz.portal.entity.QuestionComment;
import com.czz.portal.mapper.CourseConsultMapper;
import com.czz.portal.mapper.LikeMapper;
import com.czz.portal.mapper.QuestionCommentMapper;
import com.czz.portal.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: LikeServiceImpl
 * @Author Czz
 * @Date 2020/2/13
 * @Time 11:03
 **/
@Service
public class LikeServiceImpl extends ServiceImpl<LikeMapper, Like> implements LikeService {

    @Autowired
    LikeMapper likeMapper;

    @Autowired
    CourseConsultMapper courseConsultMapper;

    @Autowired
    QuestionCommentMapper commentMapper;

    @Override
    public Boolean consultLike(String userId, String consultId) {
        //判断用户是否点过赞
        QueryWrapper<Like> likeQueryWrapper = new QueryWrapper<>();
        likeQueryWrapper.eq("user_id",userId);
        likeQueryWrapper.eq("target_id",consultId);
        likeQueryWrapper.eq("like_type",0);
        Like like = likeMapper.selectOne(likeQueryWrapper);

        CourseConsult consult = new CourseConsult();
        consult.setConsultId(consultId);

        //点过赞，取消点赞
        if (!ObjectUtils.isEmpty(like)){
            likeMapper.deleteById(like.getLikeId());
            courseConsultMapper.consultLikeReduce(consultId);
            return false;
        }

        //没点过赞
        Like insert = new Like();
        insert.setGmtCreate(new Date()).setType(0).setTargetId(consultId).setUserId(userId);
        likeMapper.insert(insert);
        courseConsultMapper.consultLikePlus(consultId);
        return true;
    }

    @Override
    public Boolean commentLike(String userId, String commentId) {
        //判断用户是否点过赞
        QueryWrapper<Like> likeQueryWrapper = new QueryWrapper<>();
        likeQueryWrapper.eq("user_id",userId);
        likeQueryWrapper.eq("target_id",commentId);
        likeQueryWrapper.eq("like_type",1);
        Like like = likeMapper.selectOne(likeQueryWrapper);

        QuestionComment comment = new QuestionComment();
        comment.setCommentId(commentId);

        //点过赞，取消点赞
        if (!ObjectUtils.isEmpty(like)){
            likeMapper.deleteById(like.getLikeId());
            commentMapper.commentLikeReduce(commentId);
            return false;
        }

        //没点过赞
        Like insert = new Like();
        insert.setGmtCreate(new Date()).setType(1).setTargetId(commentId).setUserId(userId);
        likeMapper.insert(insert);
        commentMapper.commentLikePlus(commentId);
        return true;
    }

    @Override
    public List<String> getUserCommentLike(String id) {
        QueryWrapper<Like> likeQueryWrapper = new QueryWrapper<>();
        likeQueryWrapper.eq("user_id",id);
        likeQueryWrapper.eq("like_type",1);
        List<Like> list = likeMapper.selectList(likeQueryWrapper);

        List<String> likeList = new ArrayList<>();

        list.forEach(like -> {
            likeList.add(like.getTargetId());
        });

        return likeList;
    }

    @Override
    public List<String> getUserConsultLike(String id) {
        QueryWrapper<Like> likeQueryWrapper = new QueryWrapper<>();
        likeQueryWrapper.eq("user_id",id);
        likeQueryWrapper.eq("like_type",0);
        List<Like> list = likeMapper.selectList(likeQueryWrapper);

        List<String> likeList = new ArrayList<>();

        list.forEach(like -> {
            likeList.add(like.getTargetId());
        });

        return likeList;
    }
}
